﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Calligraphy.CalligraphyData;

namespace Calligraphy.Data
{
    class AuthorDataSource
    {
        private static AuthorDataSource _dataSource = new AuthorDataSource();

        private ObservableCollection<T_Author> _allAuthors = new ObservableCollection<T_Author>();
        public ObservableCollection<T_Author> AllAuthors
        {
            get
            {
                return _allAuthors;
            }
        }



        public static IEnumerable<T_Author> GetAuthors()
        {
            return _dataSource.AllAuthors;
        }

        public static T_Author GetAuthor(int id)
        {
            var matches = _dataSource.AllAuthors.Where((a) => a.ID == id);

            if (matches.Count() == 1)
                return matches.First<T_Author>();

            return null;
        }

        //public static T_Rubbing GetRubbing(int rubbingId)
        //{
        //    var q = from a in _dataSource._allAuthors from r in a.T_Rubbing where r.ID == rubbingId select r;
        //    return q.First<T_Rubbing>();
        //}

        //public static T_Rubbing GetRubbingAndLoadingRubbingPhoto(int rubbingId)
        //{
        //    var q = from a in _dataSource._allAuthors from r in a.T_Rubbing where r.ID == rubbingId select r;
        //    T_Rubbing rubbing = q.First<T_Rubbing>();
        //    if (rubbing.T_RubbingPhoto.Count == 0)
        //    {
        //        var qRubbingPhoto = from rp in DataServiceContextHelper.DataContext.T_RubbingPhoto.Expand("T_Photo").Expand("T_Rubbing") where rp.RubbingID == rubbingId select rp;
        //        rubbing.T_RubbingPhoto.LoadAsync(qRubbingPhoto);
        //    }
        //    return rubbing;
        //}

        //public static T_RubbingPhoto GetRubbingPhoto(int rubbingPhotoId)
        //{
        //    var q = from a in _dataSource.AllAuthors from r in a.T_Rubbing from rp in r.T_RubbingPhoto where rp.ID == rubbingPhotoId select rp;
        //    return q.First<T_RubbingPhoto>();
        //}
        

        public AuthorDataSource()
        {
            LoadAuthors();
        }

        private async void LoadAuthors()
        {
            //DataLoadOptions dlo = new DataLoadOptions();
            //dlo.LoadWith<Customer>(c => c.Orders);
            //db.LoadOptions = dlo;

            IEnumerable<T_Author> authors =
                await
                ((DataServiceQuery<T_Author>)DataServiceContextHelper.DataContext.T_Author.Expand("T_Dynast").Expand("T_Photo").Where(a=>a.IsDeleted == null || a.IsDeleted == false).Take(200)).ExecuteAsync<T_Author>();

            foreach (var a in authors)
            {
                var qr = from r in DataServiceContextHelper.DataContext.T_StelePaper.Expand("T_Author").Expand("T_Photo").Expand("T_Dynast").Expand("T_Style") where (r.IsDeleted == null || r.IsDeleted == false) && r.AuthorID == a.ID select r;
                a.T_StelePaper.LoadAsync(qr);
                
                _allAuthors.Add(a);
            }
        }
    }
}
